<template>
	<view class="page-container">
		<!-- 顶部标题和搜索框 -->
		<view class="header">
		<!-- 	<text class="header-title">{{pageTitle}}</text> -->
			<view class="search-container">
				<view class="search-box">
					<text class="search-icon"></text>
					<input class="search-input" type="text" placeholder="搜索智能锁..." v-model="searchKeyword" @input="handleSearch"/>
				</view>
			</view>
		</view>
		<view class="container-img">
			<image class="img" src="/static/img/indexiocn.png"/>
		</view>
<!-- 		<view class="container-h1">
			<view><image class="h1-img" src="/static/img/huangguan.png"></image></view>
			<view class="h1-text">门锁精选</view>
		</view> -->
		<!-- 智能锁列表 -->
		<view class="lock-list">
			<view class="lock-item" v-for="(lock, index) in filteredLockList" :key="index" @click="resser(lock)">
			<!-- 	<view class="lock-image-container"> -->
					<!-- <image class="lock-image" :src="lock.image"></image> -->
					<view class="lock-image">
						<swiper class="swiper"  circular :indicator-dots="indicatorDots" :autoplay="autoplay" :interval="interval"
									:duration="duration"
									
									>
									<swiper-item v-for="(item,i) in lock.images" :key="i">
									<image class="lock-images" :src="item"></image>
									</swiper-item>

								</swiper>
					</view>
					  
	                 <image class="lock-huoimage" src="/static/img/huore.png"></image>
				<!-- 	 <image class="lock-baimage" src="/static/img/61752655219_.pic_hd.png"></image> -->
			<!-- 	</view> -->
				<view class="lock-info">
					<text class="lock-title">震撼新品上市</text>
					<text class="lock-name">{{lock.title}}</text>
					<text class="lock-desc">{{lock.subtitle}}</text>
					<view class="price-button-container">
						<text class="lock-price">0元预约</text>
						<button class="buy-button">预约安装</button>
					</view>
				</view>
			</view>
			<view class="no-result" v-if="filteredLockList.length === 0">
				<text>没有找到匹配的智能锁</text>
			</view>
		</view>
	</view>
</template>

<script>
	import { getCartData } from '../../api';
	let baseUrl = 'https://door.10906.cn/api/'
	import wxFun from '@/utils/wxFun.js'
	export default {
		data() {
			return {
				pageTitle: '智能锁产品列表',
				searchKeyword: '',
				lockList: [
					{
						name: '智能锁安装',
						description: '支持指纹、密码、刷卡、钥匙、APP开锁',
						price: '0元预约',
						imageUrl: '/static/img/bjt.png',
	                    title:'震撼新品上市',
						tagColor: '#ff4444'
					},
					{
						name: '智能锁安装',
						description: '支持指纹、密码、刷卡、钥匙、APP开锁',
						price: '0元预约',
						imageUrl: '/static/img/bjt.png',
						 title:'震撼新品上市',
						tagColor: '#ff9500'
					}
				],
				 swiperList:[],
				 	   indicatorDots: true,
				 	            autoplay: true,
				 	            interval: 2000,
				 	            duration: 500
			}
		},
		async onLoad() {
			  // if (uni.getSystemInfoSync().platform === 'h5') {
			  //     // 方法1：隐藏默认导航栏（与navigationStyle:custom效果相同）
			  //     document.querySelector('.uni-page-head-hd').style.display = 'none';
			      
			  //     // 方法2：仅隐藏返回按钮（保留标题）
			  //     // const backBtn = document.querySelector('.uni-page-head-btn.uni-page-head-btn-left');
			  //     // if (backBtn) {
			  //     //   backBtn.style.display = 'none';
			  //     // }
			  //   }
			  console.log('页面加载完成');
			  	this.getlist();
			  if(localStorage.getItem("token")){
			console.log('1111111');
				  		// await this.login()
				  		// this.initdata()
			  }
			  else{
				  console.log('2222222222');
				  const code = wxFun.init();
				  		this.code = code
				  		if (code) {
				  			await this.auto_login()
				  		}
			  }
			  

		
		},
		onShow() {
			this.getlist();
		},
		computed: {
           change(e) {
			this.current = e.detail.current;
		},
			filteredLockList() {
				if (!this.searchKeyword) {
					return this.lockList;
				}
				const keyword = this.searchKeyword.toLowerCase();
				return this.lockList.filter(lock => {
					return lock.name.toLowerCase().includes(keyword) || 
						   lock.description.toLowerCase().includes(keyword);
				});
			}
		},
		methods: {
			// 抽奖登录
			resser(lock){
				   
				      console.log('lock',lock.id)
				      // 3. 路由跳转并传递参数
					   // const objStr = JSON.stringify(lock.id);
				      uni.navigateTo({
				        url: `/pages/my/components/reservation?userInfo=${lock.id}`
				      });
			},
	async auto_login() {
		try {
			let res = await uni.request({
				url: baseUrl + `third/wechat/login?code=${this.code}`,
				method: 'POST',
				data: {
					platform: 'WechatOfficialAccount'
				}
			})
			// this.token = res.data.token
			console.log(res.data.data)
			this.token = res.data.data.userinfo.token
			this.$store.commit('setToken', res.data.data.userinfo.token);
			this.$store.commit('setUserInfo', res.data.data.userinfo);
			console.log(this.$store.state)
			localStorage.setItem('token', res.data.data.userinfo.token)
			localStorage.setItem('userInfo', JSON.stringify(res.data.data.userinfo))
		} catch(error) {
			// window.location.href = window.location.href.split("?")[0]
			console.log(error)
			setTimeout(() => {
				// debugger
				window.location.replace(window.location.href.split("?")[0]);
			}, 100)
		}
	},
			buyLock(index) {
				// uni.showToast({
				// 	title: '已预约: ' + this.filteredLockList[index].name,
				// 	icon: 'none'
				// });
			},
			handleSearch() {
				// 搜索逻辑已经在computed中实现
			},
			async getlist(){
				console.log('111111')
				// let token=localStorage.getItem('token');
				 let res=await getCartData();
				 this.lockList=res.data.data;
				 // this.swiperList=res.data.data.imageUrl
				 console.log('res',res.data.data);
			}
		}
	}
</script>

<style lang="scss">
	$primary-color: #e64340;
	$secondary-color: #333333;
	$light-gray: #f5f5f5;
	$border-radius: 20rpx;

	.page-container {
		padding: 0;
		background-color: $light-gray;
		min-height: 100vh;
		padding-bottom: 120rpx;
		// margin-bottom: 200rpx;
		.container-h1{
			margin-left: 296rpx;
			height: 106rpx;
			line-height: 106rpx;
			display: flex;
			// text-align: center;
			.h1-img{
				width: 30rpx;
				height: 34rpx;
			}
			.h1-text{
				margin-left: 4rpx;
				color: #D2191F;
				font-size: 28rpx;
			}
		}
.container-img{
	width: 750rpx;
	height: 318rpx;
	.img{
		width: 750rpx;
		height: 318rpx;
	}
}
		.header {
			padding: 20rpx;
			background-color: #fff;
			// border-radius: $border-radius;
			margin-bottom: 20rpx;

			.header-title {
				font-size: 36rpx;
				font-weight: bold;
				text-align: center;
				margin-bottom: 20rpx;
				// color: $secondary-color;
			}

			.search-container {
				padding: 0 20rpx;

				.search-box {
					display: flex;
					align-items: center;
					background-color: #f5f5f5;
					border-radius: 40rpx;
					padding: 16rpx 24rpx;
                        text-align: center;
					.search-icon {
						display: inline-block;
						width: 32rpx;
						height: 32rpx;
						background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="%23999"><path d="M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"/></svg>');
						background-size: contain;
						background-repeat: no-repeat;
						margin-right: 16rpx;
					}

					.search-input {
						flex: 1;
						text-align: center;
						font-size: 28rpx;
						color: #333;
						background: transparent;
						border: none;
						outline: none;
						padding: 0;
					}
				}
			}
		}

		.lock-list {
			width: 750rpx;
			padding: 15rpx;
			box-sizing: border-box;
			.lock-item {
				position: relative;
				width: 722rpx;
				height: 752rpx;
				box-sizing: border-box;
				background-color: #fff;
				border-radius: 20rpx;
				padding: 18rpx 10rpx;
				margin-bottom: 30rpx;
				box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);

				// .no-result {
				// 	text-align: center;
				// 	padding: 60rpx 0;
				// 	color: #999;
				// 	font-size: 28rpx;
				// }

				// .lock-image-container {
				// 	position: relative;
				// 	width: 200rpx;
				// 	height: 200rpx;
				// 	margin-right: 20rpx;
				// }

				.lock-image {
					width: 702rpx;
					height: 434rpx;
					// background-color: red;
					.swiper{
						width: 702rpx;
						height: 434rpx;
						.lock-images{
							width: 702rpx;
							height: 434rpx;
						}
					}
				}
                 .lock-huoimage{
					 position: absolute;
					 top: 40rpx;
					 left: 14rpx;
					 width: 206rpx;
					 height: 80rpx;
				 }
				 .lock-baimage{
					 position: absolute;
					 left: 414rpx;
					 top: 328rpx;
					 width: 308rpx;
					 height: 352rpx;
					 
				 }
				.lock-tag {
					position: absolute;
					top: 10rpx;
					left: 10rpx;
					color: white;
					font-size: 20rpx;
					padding: 5rpx 10rpx;
					border-radius: 10rpx;
				}

				.lock-info {
					flex: 1;
					display: flex;
					flex-direction: column;
                     .lock-title{
						 margin-left: 48rpx;
						 margin-top: 24rpx;
						 font-size: 28rpx;
					 }
					.lock-name {
						 margin-left: 48rpx;
						font-size: 52rpx;
						font-weight: bold;
						margin-top: 10rpx;
					}

					.lock-desc {
						 margin-left: 48rpx;
						font-size: 20rpx;
						color: #0D0F0F;
						margin-top: 10rpx;
						
					}

					.price-button-container {
						 margin-left: 48rpx;
						display: flex;
					
				
						margin-top: 28rpx;
					}

					.lock-price {
						font-size: 24rpx;
						width: 264rpx;
						height: 46rpx;
						font-weight: bold;
						line-height: 46rpx;
						background-color: #F7F8FA;
						border-radius: 24rpx;
					}
					.buy-button {
						line-height: 46rpx;
						// 	position: relative;
						// top:0;
						// left: -100rpx;
						width: 158rpx;
						height: 46rpx;
						margin-left: -150rpx;
						background-color: #e64340;
						color: #fff;
						border-radius: 24rpx;
						font-size: 26rpx;
						// padding: 10rpx 30rpx;
						border: none;
					}
				}
			}
		}
	}
</style>
